﻿using Pdelvo.Minecraft.Network;

namespace Pdelvo.Minecraft.Protocol.Packets
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    public class MobSpawn : Packet
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MobSpawn"/> class.
        /// </summary>
        /// <remarks></remarks>
        public MobSpawn()
        {
            Code = 0x18;
        }

        /// <summary>
        /// Gets or sets the entity ID.
        /// </summary>
        /// <value>The entity ID.</value>
        /// <remarks></remarks>
        public int EntityID { get; set; }
        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        /// <value>The type.</value>
        /// <remarks></remarks>
        public byte Type { get; set; }
        /// <summary>
        /// Gets or sets the X.
        /// </summary>
        /// <value>The X.</value>
        /// <remarks></remarks>
        public int X { get; set; }
        /// <summary>
        /// Gets or sets the Y.
        /// </summary>
        /// <value>The Y.</value>
        /// <remarks></remarks>
        public int Y { get; set; }
        /// <summary>
        /// Gets or sets the Z.
        /// </summary>
        /// <value>The Z.</value>
        /// <remarks></remarks>
        public int Z { get; set; }
        /// <summary>
        /// Gets or sets the yaw.
        /// </summary>
        /// <value>The yaw.</value>
        /// <remarks></remarks>
        public byte Yaw { get; set; }
        /// <summary>
        /// Gets or sets the pitch.
        /// </summary>
        /// <value>The pitch.</value>
        /// <remarks></remarks>
        public byte Pitch { get; set; }
        /// <summary>
        /// Gets or sets the head yaw.
        /// </summary>
        /// <value>The head yaw.</value>
        /// <remarks></remarks>
        public byte HeadYaw { get; set; }
        /// <summary>
        /// Gets or sets the data stream.
        /// </summary>
        /// <value>The data stream.</value>
        /// <remarks></remarks>
        public Metadata DataStream { get; set; }

        /// <summary>
        /// Receives the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        public override void Receive(BigEndianStream reader, int version)
        {
            EntityID = reader.ReadInt();
            Type = reader.ReadByte();
            X = reader.ReadInt();
            Y = reader.ReadInt();
            Z = reader.ReadInt();
            Yaw = reader.ReadByte();
            Pitch = reader.ReadByte();
            if (version >= 24)
                HeadYaw = reader.ReadByte();
            DataStream = Metadata.Read(reader, version);
        }

        /// <summary>
        /// Sends the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        protected override void Send(BigEndianStream writer, int version)
        {
            writer.Write(Code);
            writer.Write(EntityID);
            writer.Write(Type);
            writer.Write(X);
            writer.Write(Y);
            writer.Write(Z);
            writer.Write(Yaw);
            writer.Write(Pitch);
            if (version >= 24)
                writer.Write(HeadYaw);
            Metadata.WriteMetadata(DataStream, writer);
        }
    }
}